package com.cat.model.web.action.api;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cat.model.biz.service.api.LoginService;
import com.cat.model.common.model.common.DeviceInfoVO;
import com.cat.model.common.model.common.UserTokenVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.action.base.BaseAction;

public class AuthAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3474726999812086035L;
	
	private String deviceId;
	private String phoneNo;
	private String checkCode;
	private String token;
	private String refreshToken;

	private String password;	
	
	private String deviceType;
	private String pushToken;
	private String nickName;
	private Long inviterId;//邀请人用户ID
	@Autowired
	private LoginService loginService;
	@Autowired
	private UserInfoMapper userInfoMapper;
	/**
	 * 注册设备信息
	 * */
	public void regDeviceInfo(){
		if(StringUtils.isEmpty(deviceId)  || StringUtils.isEmpty(deviceType) || StringUtils.isEmpty(pushToken) ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		DeviceInfoVO deviceInfo = new DeviceInfoVO();
		deviceInfo.setDeviceId(deviceId);
		deviceInfo.setDeviceType(deviceType);
		deviceInfo.setPushToken(pushToken);
		print(loginService.regDeviceInfo(deviceInfo));
	}
		
	/**
	 * 登录前发送校验码
	 * */
	public void preLogin(){
		if(StringUtils.isEmpty(deviceId)  || StringUtils.isEmpty(phoneNo)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.preLoginByPhone(deviceId, phoneNo, Constant.CHECK_CODE_SMS));
	}	
	
	
	/**
	 * 登录前发送语音验证码
	 * */
	public void preLoginCall(){
		if(StringUtils.isEmpty(deviceId)  || StringUtils.isEmpty(phoneNo)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.preLoginByPhone(deviceId, phoneNo, Constant.CHECK_CODE_CALL));
	}
	
	/**
	 * 用户注册
	 * */
	public void loginByPhone(){
		if(StringUtils.isEmpty(deviceId)  || StringUtils.isEmpty(phoneNo) || 
				StringUtils.isEmpty(checkCode)  || StringUtils.isEmpty(password)
				||StringUtils.isEmpty(nickName) ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.loginByPhone(deviceId, phoneNo, checkCode, password,nickName,inviterId));
	}
	
	/**
	 * 登录鉴权
	 * */
	public void loginByPhonePass(){
		if(StringUtils.isEmpty(deviceId)  || StringUtils.isEmpty(phoneNo) ||  StringUtils.isEmpty(password)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.loginByPhonePass(deviceId, phoneNo, password));
	}
	
	
	/**
	 * 修改密码
	 * */
	public void updatePassword(){
		if(StringUtils.isEmpty(token) || StringUtils.isEmpty(phoneNo) ||  StringUtils.isEmpty(password)
				|| StringUtils.isEmpty(checkCode) ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		print(loginService.updatePassword(userToken.getUserId(),phoneNo,password,checkCode));
	}
	
	
	
	/**
	 * token续期
	 * */
	public void renewalToken(){
		if(StringUtils.isEmpty(refreshToken) ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.renewalToken(refreshToken));
	}
	
	/**
	 * 注销
	 * */
	public void logout(){
		if(StringUtils.isEmpty(token) ){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.logout(token));
	}
	
	/**
	 * 发送校验码
	 * */
	public void preBindPhone(){
		if(StringUtils.isEmpty(token) || StringUtils.isEmpty(phoneNo)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.preBindPhoneNo(token, phoneNo, Constant.CHECK_CODE_SMS));
	}
	
	/**
	 * 发送语音校验码
	 * */
	public void preBindPhoneCall(){
		if(StringUtils.isEmpty(token) || StringUtils.isEmpty(phoneNo)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.preBindPhoneNo(token, phoneNo, Constant.CHECK_CODE_CALL));
	}

	/**
	 * 重新绑定手机号码
	 * */	
	public void bindPhoneNo(){
		if(StringUtils.isEmpty(token)  || StringUtils.isEmpty(checkCode)  || StringUtils.isEmpty(phoneNo) ||  StringUtils.isEmpty(password)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(loginService.bindPhoneNo(token, phoneNo, checkCode, password));
	}
	
	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getRefreshToken() {
		return refreshToken;
	}

	public void setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public String getToken() {
		return token;
	}


	public void setToken(String token) {
		this.token = token;
	}

	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getDeviceType() {
		return deviceType;
	}


	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}


	public String getPushToken() {
		return pushToken;
	}


	public void setPushToken(String pushToken) {
		this.pushToken = pushToken;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public Long getInviterId() {
		return inviterId;
	}

	public void setInviterId(Long inviterId) {
		this.inviterId = inviterId;
	}

}
